package com.dengdai.applibrary.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dengdai.applibrary.R;
import com.dengdai.applibrary.base.BaseApplication;

/**
 * @说明 自定义对话框 两个按钮带标题
 * @作者 LY
 * @时间 2016/1/4 15:56
 * @版权 Copyright(c) 2016 LY-版权所有
 */
public class CustomAlertView extends Dialog {
    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvLeftBtn;
    private TextView tvRightBtn;

    private AlertOnClick alertOnClick;


    public CustomAlertView(Context context) {
        super(context);
        initView();
    }

    /**
     * 显示对话框信息
     *
     * @param title        标题
     * @param content      内容
     * @param leftName     左边按钮名称
     * @param rightName    右边按钮名称
     * @param isCancelable 是否可取消的
     */
    public void showAlertInfo(int title, int content, int leftName, int rightName, boolean isCancelable) {
        showAlertInfo(BaseApplication.getAppContext().getResources().getString(title),
                BaseApplication.getAppContext().getResources().getString(content),
                BaseApplication.getAppContext().getResources().getString(leftName),
                BaseApplication.getAppContext().getResources().getString(rightName),
                isCancelable);
    }


    /**
     * 显示对话框信息
     *
     * @param title        标题
     * @param content      内容
     * @param leftName     左边按钮名称
     * @param rightName    右边按钮名称
     * @param isCancelable 是否可取消的
     */
    public void showAlertInfo(String title, String content, String leftName, String rightName, boolean isCancelable) {
        tvTitle.setText(title);
        tvContent.setText(content);
        tvLeftBtn.setText(leftName);
        tvRightBtn.setText(rightName);
        tvLeftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (alertOnClick != null) {
                    alertOnClick.onLeftClick();
                    dismiss();
                }
            }
        });
        tvRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (alertOnClick != null) {
                    alertOnClick.onRightClick();
                    dismiss();
                }
            }
        });
        setCancelable(isCancelable);
        show();
    }


    /**
     * 初始化控件
     */
    public void initView() {
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.common_custom_alert_view, null);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvContent = (TextView) view.findViewById(R.id.tv_content);
        tvLeftBtn = (TextView) view.findViewById(R.id.tv_left_btn);
        tvRightBtn = (TextView) view.findViewById(R.id.tv_right_btn);
        //设置没有标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        //设置动画
//        getWindow().setWindowAnimations(R.style.dialogAnim);
        //设置背景颜色
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        addContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    public interface AlertOnClick {
        /**
         * 左边按钮事件
         */
        void onLeftClick();

        /**
         * 右边按钮事件
         */
        void onRightClick();
    }

    /**
     * 设置对话框监听事件
     *
     * @param alertOnClick 事件
     */
    public void setAlertOnClick(AlertOnClick alertOnClick) {
        this.alertOnClick = alertOnClick;
    }
}
